package com.liaoban.app.ui.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.liaoban.app.R;
import com.liaoban.app.api.type.Grade;
import com.liaoban.app.biz.CommonBiz;
import com.liaoban.app.constants.ConfigKey;
import com.liaoban.app.helper.ConfigHelper;
import com.liaoban.app.ui.base.BaseToolbarActivity;
import com.liaoban.app.util.CommonUtils;
import com.liaoban.app.widget.ProgressView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindInt;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ybc on 2018/8/2 0002.
 * 等级详情
 */
public class GradeDetailActivity extends BaseToolbarActivity {
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.view_progress)
    ProgressView progressView;
    @BindView(R.id.tv_two)
    TextView tvTwo;
    @BindView(R.id.tv_three)
    TextView tvThree;
    @BindView(R.id.iv_vip_left)
    ImageView ivVipLeft;
    @BindView(R.id.iv_vip_right)
    ImageView ivVipRight;
    @BindView(R.id.tv_now_grade)
    TextView tvNowGrade;
    @BindView(R.id.tv_total_grade)
    TextView tvTotalGrade;

    private List<Grade> mGrades;
    private int mPos;

    public static Intent newIntent(Context context) {
        Intent intent = new Intent(context, GradeDetailActivity.class);
        return intent;
    }

    @Override
    protected String getNewTitle() {
        return "等级详情";
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_grade_detail;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mPos = Integer.parseInt(ConfigHelper.getString(this, ConfigKey.USER_GRADE));
        List<String> parts2 = new ArrayList<>();
        parts2.add("1");
        parts2.add("10");
        CommonUtils.modifyMultiColor(tvTwo, "用户每消费1元人民币等价聊币，即可获得10点经验值", parts2, getResources().getColor(R.color.btn_yellow));
        List<String> parts3 = new ArrayList<>();
        parts3.add("1");
        parts3.add("10");
        CommonUtils.modifyMultiColor(tvThree, "用户每收入1元人民币，即可获得10点经验值", parts3, getResources().getColor(R.color.btn_yellow));
        showRunningDialog();
        startTask(CommonBiz.getInstance().grade(), listBaseResponse -> {
            mGrades = listBaseResponse.data;
            if (!CommonUtils.isEmpty(mGrades)) {
                setView();
            }
        });

    }

    private void setView() {
        Grade grade = mGrades.get(mPos);
        CommonUtils.modifyColor(tvName, "升级还需要" + grade.getNext_grade() + "点", "" + grade.getNext_grade(), getResources().getColor(R.color.btn_yellow));
        progressView.setCurrentCount(grade.getNow_grade() + grade.getNext_grade(), grade.getNow_grade());
        ivVipLeft.setImageResource(CommonUtils.getLargeGradeRes(String.valueOf(mPos)));
        ivVipRight.setImageResource(CommonUtils.getLargeGradeRes(String.valueOf(mPos + 1)));
        tvNowGrade.setText(grade.getNow_grade() + "");
        tvTotalGrade.setText(String.valueOf(grade.getNow_grade() + grade.getNext_grade()));
    }

    @OnClick(R.id.iv_left1)
    public void left1() {
        if (mPos == 0) {
            return;
        }
        mPos--;
        setView();
    }

    @OnClick(R.id.iv_right1)
    public void right1() {
        if ((mPos + 1) >= mGrades.size()) {
            return;
        }
        mPos++;
        setView();
    }

}
